<?php

namespace App\Http\Resources;

use App\Models\JobpositionModel;
use App\Models\PlazaModel;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Support\Collection;
use SuperEggs\DcatDistpicker\DcatDistpickerHelper;

class JobpositionDetailResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *职位详情
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'position_name' => $this->position_name,
            'salary' => (string)$this->salary,
            'salary_type' => JobpositionModel::SALARY_TYPE_LIST[$this->salary_type],
            'gender_requirement' => JobpositionModel::GENDER_LIST[$this->gender_requirement],
            'age_restrict' => $this->age($this->min_age, $this->max_age),
            'tags' => $this->tags,
            'label' => JobpositionModel::LABEL_LIST[$this->label] ?? '',
            'title_tag' => JobpositionModel::TITLE_TAG[$this->title_tag] ?? '',
//            'work_location' => $this->work_location,
//            'work_date' => $this->work_date,
            'company_name' => $this->company->company_name ?? '',
            'contact_person_avatar' => $this->company->contactPersonUrl(),
            'company_logo' => $this->company->logoUrl(),
            'contact_person_name' => $this->company->contact_person_name ?? '',
            'work_experience' => JobpositionModel::WORK_EXPERIENCE_LIST[$this->work_experience] ?? '',
            'job_description' => $this->job_description,
            'experience_requirement' => $this->experience_requirement,
            'region' => $this->region(),
            'is_apply' => $this->is_apply(auth('api')->user()),
            'is_collect' => $this->is_collect(auth('api')->user()),
            'education' => JobpositionModel::GENDER_EDUCATION[$this->education] ?? '',
        ];
    }

    private function age($min, $max): string
    {
        if ($min <= 0 && $max <= 0) {
            return '年龄不限';
        }
        if ($min >= 0 && $max >= 0) {
            return "$min-{$max}周岁";
        }
        if ($min <= 0 && $max >= 0) {
            return "0-{$max}周岁";
        }
        if ($min >= 0 && $max <= 0) {
            return "{$max}周岁以上";
        }
        return "";
    }

    private function region()
    {
        if (!$this->region_region){
            return '不限地点';
        }
        return DcatDistpickerHelper::getAreaName($this->region_city) . DcatDistpickerHelper::getAreaName($this->region_region);
    }
}
